<style>
    
</style>
<template>
    <div>
        <header class="admin-container-header clearfix">
            <div class="header-group">
                <h4 class="title">内容</h4> <h5 class="sub-title">SEO推送（请先配置相应的推送接口地址）</h5>
            </div>
        </header>
        <main class="admin-container-body" style="height: calc(100% - 50px)">
            <aside class="side-box panel-box" style="height: calc(100% - 50px - 92px);bottom: 15px;">
                <section class="side-box-body">
                    <section class="side-list">
                        <ul>
                            <li class="nav-item" :class='{"active" : item.active}' v-for='item in domainList'>
                                <a @click="menuClick(item)">{{item.domain}}</a>
                            </li>
                        </ul>
                    </section>
                </section>
            </aside>
            <section class="content-box" >
            <section class="panel-box">
                <section class="panel-box-body">
                    <!--内容列表-->
                    <section class="diy-table-list" v-cloak>
                        <div class="content-list-header d-flex justify-content-between">
                            <div>
                                <i-input v-model="searchData" placeholder="请输入搜索的关键词" style='width: 300px;'>
                                    <i-Button slot="append" icon="ios-search" @click='itemSearchClick'></i-Button>
                                </i-input>
                            </div>
                            <div>
                                <button class="ivu-btn ivu-btn-text" type="text" :disabled='!setting.baiduMipApi || !itemListSelectStatus' @click="pushModal('baiduMip')">百度MIP推送</button>
                                <button class="ivu-btn ivu-btn-text" type="text" :disabled='!setting.baiduTimeApi || !itemListSelectStatus' @click="pushModal('baiduTime')"> 百度链接推送</button>
                                <button class="ivu-btn ivu-btn-text" type="text" :disabled='!setting.smApi || !itemListSelectStatus' @click="pushModal('sm')"> 神马MIP推送</button>
                                <button class="ivu-btn ivu-btn-text" type="text" :disabled='!setting.xzhRealtimeApi || !itemListSelectStatus' @click="pushModal('realtime')">熊掌号天级推送</button>
                                <button class="ivu-btn ivu-btn-text" type="text" :disabled='!setting.xzhBatchApi || !itemListSelectStatus' @click="pushModal('batch')">熊掌号周级推送</button>
                            </div>
                        </div>
                        <table class="ssy-table">
                        	<thead>
	                        	<tr>
	                        		<th>
	                                    <Checkbox v-model="itemListSelectStatus"  :disabled='!itemList.length' @on-change='itemListSelectChange'>全选</Checkbox>
	                        		</th>
	                        		<th>标题</th>
	                        		<th>
	                        			<Dropdown trigger="click" @on-click='changeClick'>
	                                        <a href="javascript:void(0)">百度MIP
	                                            <Icon type="ios-arrow-down"></Icon>
	                                        </a>
	                                        <DropdownMenu slot="list">
	                                            <DropdownItem name='mipOk'>已推送</DropdownItem>
	                                            <DropdownItem name='mipNo'>未推送</DropdownItem>
	                                        </DropdownMenu>
	                                    </Dropdown>
	                        		</th>
	                        		<th>
	                        			<Dropdown trigger="click" @on-click='changeClick'>
	                                        <a href="javascript:void(0)">百度链接
	                                            <Icon type="ios-arrow-down"></Icon>
	                                        </a>
	                                        <DropdownMenu slot="list">
	                                            <DropdownItem name='linkOk'>已推送</DropdownItem>
	                                            <DropdownItem name='linkNo'>未推送</DropdownItem>
	                                        </DropdownMenu>
	                                    </Dropdown>
	                        		</th>
	                        		<th>
	                        			<Dropdown trigger="click" @on-click='changeClick'>
	                                        <a href="javascript:void(0)">神马MIP
	                                            <Icon type="ios-arrow-down"></Icon>
	                                        </a>
	                                        <DropdownMenu slot="list">
	                                            <DropdownItem name='smOk'>已推送</DropdownItem>
	                                            <DropdownItem name='smNo'>未推送</DropdownItem>
	                                        </DropdownMenu>
	                                    </Dropdown>
	                        		</th>
	                        		<th>
	                        			<Dropdown trigger="click" @on-click='changeClick'>
	                                        <a href="javascript:void(0)">抓取次数
	                                            <Icon type="ios-arrow-down"></Icon>
	                                        </a>
	                                        <DropdownMenu slot="list">
	                                            <DropdownItem name='collectOk'>已抓取</DropdownItem>
	                                            <DropdownItem name='collectNo'>未抓取</DropdownItem>
	                                        </DropdownMenu>
	                                    </Dropdown>
	                        		</th>
	                        		<th>抓取时间</th>
	                        	</tr>
                        	</thead>
                        	<tbody>
	                        	<tr v-if='itemList.length' class="table-tr" v-for='item in itemList'>
	                        		<td><Checkbox v-model="item.itemListSelectStatus"></Checkbox></td>
	                        		<td><span><a :href="item.url" target="_blank">{{item.title}}</a></span></td>
	                        		<td>{{item.mip_push_num}}</td>
	                        		<td>{{item.link_push_num}}</td>
	                        		<td>{{item.sm_push_num}}</td>
	                        		<td>{{item.baidu_spider_num}}</td>
	                        		<td><span v-if='item.baidu_spider_time'>{{item.baidu_spider_time | time}}</span></td>
	                        	</tr>
	                        	<tr v-if='!itemList.length'>
                        			<td colspan="100">
		                        		<div class="no-block">
				                            <Icon type="ios-filing-outline"></Icon>
				                            <p>暂无数据</p>
				                        </div>
                        			</td>
	                        	</tr>
                        	</tbody>
                        </table>
                    </section>
                    <!--内容列表结束-->
                    <!--分页-->
                    <div class="text-right clearfix mt-3">
                        <Page :total="pagination.total"  @on-page-size-change='itemPaginationSelect' :page-size-opts='[10,100,500,1000,5000]' show-total show-sizer placement='top' @on-change='itemPaginationClick'></Page>
                    </div>
                </section>
            </section>
         
        </section>
    </main>   
     <!--推送-->
    <Modal title="推送" v-model="push.pushStatus">
    	 <i-form :label-width="80">
            <Form-Item label="数量">
            	{{push.pushData.length}}篇
            </Form-Item>
            <Form-Item label="列表" class='push-list-warp'> 
            	<ul class="list-unstyled push-list">
                	<li class="text-999 font-12" style="line-height: 20px;" v-for='item in push.pushData'>{{item.url}}</li>
            	</ul>
            </Form-Item>
            <Form-Item label="接口">
            	{{push.api}}
            </Form-Item>
            <Form-Item label="进度">
                <i-Progress :percent="scale"></i-Progress>
            </Form-Item>
        </i-form>
        <span slot="footer" class="dialog-footer">
            <i-button @click="push.pushStatus = false">取 消</i-button>
            <i-button type="primary" @click='pushPost(0)'>确 定</i-button>
          </span>
    </Modal>
    
    </div>
</template>

<script>
export default {
    data () {
       return {
            searchData: '',
            itemListSelectStatus: false,
            itemList: [], 
            scale: 0,
            //域名列表
            domainList: [],
            currentDomainId: '',
            setting: '',
           //推送
            push: {
                pushStatus: false,
                pushData: [],
                pushType: '',
                api: ''
            },
            pagination: {
                currentPage: 1,
                limit: 10,
                total: this.total,
            },
            
        }
    },
    watch: {
        itemList: {
            handler(newValue, oldValue) {
                var tempStatus = false;
    　　　　　for (var i = 0; i < this.itemList.length; i ++) {
                if (this.itemList[i].itemListSelectStatus == true) {
                    tempStatus = true;
                }
            }
            if (tempStatus) {
                this.itemListSelectStatus = true;
            } else {
                this.itemListSelectStatus = false;
            }
    　　　　},
    　　　　deep: true
    　　}
    },
    mounted() {
        this.getSetting();
        this.getDomainList();
    },
    methods: {
        getDomainList() {
            this.$ssy.ajax('{$domain}/setting/ApiAdminDomain/itemList', {
                page: 1,
                limit: 10000,
            }).then(res => {
                this.itemList = [];
                if(res.code == 1) {
                    var domainList = res.data.itemList;
                    var tempCategoryList = [{id:0,active: true,domain: '默认域名'}];
                    if (domainList && domainList.length) {
                        for (var i = 0; i < domainList.length; i++) {
                            domainList[i].active = false;
                            tempCategoryList.push(domainList[i]);
                        }
                    }
                    this.domainList = tempCategoryList;
                }
            });
        },
        changeClick(val) {
            this.type = val;
            this.getItemList();
        },
        getSetting: function() {
            this.loading = true;
            this.$ssy.ajax('{$domain}/setting/ApiAdminSetting/settingSelect', {

            }).then(res => {
                this.loading = false;
                if(res.code == 1) {
                    var temp = res.data;
                    this.setting = temp;
                    this.getItemList();
                }
            });
        },
        
        getUrl() {
            var itemUrlList = [];
            for(var i = 0; i < this.itemList.length; i++) {
                if (this.itemList[i].itemListSelectStatus) {
                    itemUrlList.push({'url': this.itemList[i].url,'id': this.itemList[i].id});
                }
            }
            return itemUrlList;
        },
        pushModal(type) {
            this.scale = 0;
            this.countList = [];
            this.push.pushType = type;
            this.push.pushData = this.getUrl();
            if (this.push.pushType == 'baiduMip') {
                this.push.api = this.setting.baiduMipApi;
            }
            if (this.push.pushType == 'baiduTime') {
                this.push.api = this.setting.baiduTimeApi;
            }
            if (this.push.pushType == 'sm') {
                this.push.api = this.setting.smApi;
            }
            if (this.push.pushType == 'realtime') {
                this.push.api = this.setting.xzhRealtimeApi;
            }
            if (this.push.pushType == 'batch') {
                this.push.api = this.setting.xzhBatchApi;
            }
            this.push.pushStatus = true;
        },
        
        pushPost(index) {
            if (!this.push.pushData.length) {
                this.$Message.error('请选择要推送的内容链接');
                return false;
            }
            if (!this.push.pushType) {
                this.$Message.error('系统无法识别你推送的类型');
                return false;
            }
            
            var scale = parseInt(index / (parseInt(this.push.pushData.length)) * 100);
            if (scale < 100) {
                this.scale = scale;
            } else {
                this.scale = 100;
            }
            if (parseInt(index) + 1 > this.push.pushData.length) {
                this.$Message.success('操作完毕');
                this.getItemList();
                return false;
            }
            
            this.$ssy.ajax('{$domain}/article/ApiAdminArticlePush/urlPost', {
                url: this.push.pushData[index]['url'],
                id: this.push.pushData[index]['id'],
                api: this.push.api,
                pushType: this.push.pushType,
            }).then(res => {
                if(res.code == 1) {
                    var resMIp = JSON.parse(res.msg);
                    if (this.push.pushType == 'baiduMip') {
                        if (resMIp.not_valid) {
                            this.$Message.error('推送的网址不合法');
                        }
                        if (resMIp.not_same_site) {
                            this.$Message.error('推送的地址，与百度接口中的域名不匹配');
                        }
                        if (resMIp.success_mip) {
                            this.$Message.success('推送成功：' + resMIp.success_mip + '条；剩余:' + resMIp.remain_mip + '条');
                        }
                    }
                    if (this.push.pushType == 'baiduTime') {
                        this.$Message.success('推送成功：' + resMIp.success + '条');
                    }
                    if (this.push.pushType == 'sm') {
                        this.$Message.success('推送成功');
                    }
                    if (this.push.pushType == 'realtime') {
                        this.$Message.success('推送成功：' + resMIp.success_realtime + '条；剩余:' + resMIp.remain_realtime + '条');
                    }
                    if (this.push.pushType == 'batch') {
                        this.$Message.success('推送成功：' + resMIp.success_batch + '条；剩余:' + resMIp.remain_batch + '条');
                    }
                    this.pushPost(index+1);
                }
            });
        },
        itemListSelectChange() {
            var itemList = this.itemList;
            for (var i = 0; i < itemList.length; i++) {
                if (this.itemListSelectStatus) {
                    itemList[i].itemListSelectStatus = true;
                } else {
                    itemList[i].itemListSelectStatus = false;
                }
            }
            this.itemList = itemList;
        },
        getItemList() {
            this.loading = true;
            var tempDomain = '';
            if (this.currentDomainId) {
                for (var i = 0; i < this.domainList.length; i++) {
                   if (this.domainList[i].id == this.currentDomainId) {
                       tempDomain = this.domainList[i].http_type + this.domainList[i].domain;
                   }
                }
            }
            this.$ssy.ajax('{$domain}/article/ApiAdminArticle/itemList', {
                domain: tempDomain,
                type: this.type,
                status: this.status,
                page: this.pagination.currentPage,
                limit: this.pagination.limit,
                keywords: this.searchData,
                siteId: this.superSiteId,
            }).then(res => {
                this.itemList = [];
                if(res.code == 1) {
                    var itemList = res.data.itemList;
                    if (itemList) {
                        for (var i = 0; i < itemList.length; i++) {
                            itemList[i].itemListSelectStatus = false; 
                        }
                        this.itemList = itemList;
                    }
                    this.pagination.total = res.data.total;
                }
                this.loading = false;
            });
        },
        itemPaginationSelect(val) {
            this.pagination.limit = val;
            this.getItemList();
        },
        itemPaginationClick(val) {
            this.pagination.currentPage = val;
            this.getItemList();
        },
        itemSearchClick: function() {
            this.getItemList();
        },
        menuClick(item) {
            for (var i = 0; i < this.domainList.length; i++) {
                this.domainList[i].active = false;
            }
            for (var i = 0; i < this.domainList.length; i++) {
                if (item.id == this.domainList[i].id) {
                    this.domainList[i].active = true;
                }
            }
            this.currentDomainId = item.id;
            if (item.id != 0) {
                this.setting = item.data;
            }
            this.pagination.currentPage = '1';
            this.getItemList();
        },
    }
}
</script>
